// MongoDB 生产环境初始化脚本

// 获取环境变量
const dbName = process.env.MONGO_INITDB_DATABASE || 'lesson-plan';
const dbUser = process.env.MONGO_APP_USERNAME || 'app';
const dbPassword = process.env.MONGO_APP_PASSWORD || 'app_password';

// 切换到应用数据库
db = db.getSiblingDB(dbName);

// 创建应用用户
db.createUser({
  user: dbUser,
  pwd: dbPassword,
  roles: [
    {
      role: 'readWrite',
      db: dbName
    }
  ]
});

// 创建集合和索引
print('Creating collections and indexes...');

// 用户集合索引
db.users.createIndex({ "email": 1 }, { unique: true });
db.users.createIndex({ "username": 1 }, { unique: true });
db.users.createIndex({ "createdAt": 1 });
db.users.createIndex({ "lastLoginAt": 1 });

// 教案集合索引
db.lessonplans.createIndex({ "title": "text", "content": "text" });
db.lessonplans.createIndex({ "author": 1 });
db.lessonplans.createIndex({ "category": 1 });
db.lessonplans.createIndex({ "isPublic": 1 });
db.lessonplans.createIndex({ "createdAt": 1 });
db.lessonplans.createIndex({ "updatedAt": 1 });
db.lessonplans.createIndex({ "tags": 1 });
db.lessonplans.createIndex({ "likes": 1 });

// 分类集合索引
db.categories.createIndex({ "name": 1 }, { unique: true });
db.categories.createIndex({ "parentId": 1 });
db.categories.createIndex({ "order": 1 });

// 文件集合索引
db.files.createIndex({ "filename": 1 });
db.files.createIndex({ "uploadBy": 1 });
db.files.createIndex({ "uploadDate": 1 });
db.files.createIndex({ "fileType": 1 });

// 通知集合索引
db.notifications.createIndex({ "recipient": 1 });
db.notifications.createIndex({ "read": 1 });
db.notifications.createIndex({ "createdAt": 1 });
db.notifications.createIndex({ "type": 1 });

// 评论集合索引
db.comments.createIndex({ "lessonPlan": 1 });
db.comments.createIndex({ "author": 1 });
db.comments.createIndex({ "createdAt": 1 });

// 活动日志集合索引
db.activities.createIndex({ "user": 1 });
db.activities.createIndex({ "type": 1 });
db.activities.createIndex({ "createdAt": 1 });
db.activities.createIndex({ "targetId": 1 });

// 版本控制集合索引
db.lessonplanversions.createIndex({ "lessonPlan": 1 });
db.lessonplanversions.createIndex({ "version": 1 });
db.lessonplanversions.createIndex({ "createdAt": 1 });

// 创建默认数据
print('Creating default data...');

// 创建默认分类
db.categories.insertMany([
  {
    _id: ObjectId(),
    name: "语文",
    description: "语文教学教案",
    order: 1,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    _id: ObjectId(),
    name: "数学",
    description: "数学教学教案", 
    order: 2,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    _id: ObjectId(),
    name: "英语",
    description: "英语教学教案",
    order: 3,
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    _id: ObjectId(),
    name: "科学",
    description: "科学教学教案",
    order: 4,
    createdAt: new Date(),
    updatedAt: new Date()
  }
]);

// 设置数据库配置
db.runCommand({
  "collMod": "users",
  "validator": {
    "$jsonSchema": {
      "bsonType": "object",
      "required": ["email", "username", "password"],
      "properties": {
        "email": {
          "bsonType": "string",
          "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
        },
        "username": {
          "bsonType": "string",
          "minLength": 3,
          "maxLength": 30
        }
      }
    }
  }
});

print('MongoDB production setup completed successfully!');
print(`Database: ${dbName}`);
print(`Application user: ${dbUser}`);
print('Indexes and default data created.');